import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterdemo/common/storage.dart';

class LocaStroge extends StatefulWidget {
  @override
  _LocaStrogeState createState() => _LocaStrogeState();
}

class _LocaStrogeState extends State<LocaStroge> {
  final myController = TextEditingController();
  String val = '';
  @override
  void dispose() {
    super.dispose();
    myController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('本地存储'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          TextField(
            controller: myController,
            decoration: new InputDecoration(
              labelText: '请输入要保存的值',
            ),
            autofocus: false,
          ),
          Text(val),
          RaisedButton(
            child: Text('保存输入的数据'),
            onPressed: () async {
              await Storages().setString('key', myController.text);
              print(myController.text);
            },
          ),
          RaisedButton(
            child: Text('获取保存的值'),
            onPressed: () {
              Storages().getStorage('key').then((v) {
                print(v);
                setState(() {
                  val = v;
                });
              });
            },
          )
        ],
      ),
    );
  }
}
